<?php
/**
 * 充值
 * User: guojing
 * Date: 2020/4/10
 * Time: 0:00
 */
namespace app\api\controller;


use app\api\model\RechargeOffline;
use app\common\controller\Api;
use think\Db;
use think\Exception;
use app\api\model\RechargeOfflineBank;
use app\api\model\User as UserModel;

class Recharge extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = '*';

    /**
     * 充值首页
     */
    public function index()
    {
        $userId = UserModel::getCurrUid($this->request->param('user_id', 0));
        $page = $this->request->param('page', 1);
        $pageSize = $this->request->param('page_size', 20);
        $params = [
            'page' => $page,
            'page_size' => $pageSize,
            'uid' => $userId
        ];
        //返回充值的收款历史记录
        $banks = RechargeOfflineBank::dataList($params);
        $res = [
            'send' => $banks ? $banks : [],
            'has_more' => count($banks) >= $pageSize ? 1 : 0,
            'receive' => RechargeOffline::receiveBank()
        ];
        $this->success('success', $res);
    }

    /**
     * 线下充值
     */
    public function offline()
    {
        $userId = UserModel::getCurrUid($this->request->param('user_id', 0));
        if ($userId <= 0) {
            $this->error('请重新登录', [],401);
        }

        $posts = $this->request->post();
        $amount = isset($posts['amount']) && $posts['amount'] ? trim($posts['amount']) : 0;
        if (!is_numeric($amount)) { //如果不是数字
            $this->error(__('请输入正确的金额'));
        }
        $amount = (float)(sprintf('%.2f', $amount));
        if ($amount <= 0) {
            $this->error(__('请输入正确的金额'));
        }

        $sendBankName = isset($posts['send_bank_name']) ? trim($posts['send_bank_name']) : '';
        if (!$sendBankName) {
            $this->error(__('银行户名为必要信息，不能留空！'));
        }
        $sendBankNumber = isset($posts['send_bank_number']) ? trim($posts['send_bank_number']) : '';
        if (!$sendBankNumber) {
            $this->error(__('银行卡号为必要信息，不能留空！'));
        }
        $sendTime = isset($posts['send_time']) ? intval(trim($posts['send_time'])) : 0;
        if ($sendTime <= 0) {
            $this->error(__('汇款日期为必要信息，不能留空！'));
        }

        //收款方
        $receiveBank = RechargeOffline::receiveBank();

        $data = [
            'send_uid' => $userId,
            'amount' => $amount,
            'send_bank_name' => $sendBankName,
            'send_bank_number' => $sendBankNumber,
            'send_time' => $sendTime,
            'receive_bank_name' => $receiveBank['receive_bank_name'],
            'receive_uname' => $receiveBank['receive_uname'],
            'receive_bank_no' => $receiveBank['receive_bank_no'],
            'receive_bank_number' => $receiveBank['receive_bank_number']
        ];
        $offline = RechargeOffline::addRecord($data);
        if (!$offline) {
            $this->error(__('操作失败！'));
        }

        // 查询打款银行是否添加过
        $bankLog = RechargeOfflineBank::findInfoByCondition(['send_uid' => $userId, 'send_bank_number' => $sendBankNumber]);
        if (!$bankLog) {
            $bankData = [
                'send_uid' => $userId,
                'send_bank_name' => $sendBankName,
                'send_bank_number' => $sendBankNumber
            ];
            //添加到历史记录
            $bankLog = RechargeOfflineBank::addRecord($bankData);
        }

        $this->success("操作成功！请等待后台审核！");
    }

}